<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="queryParams.documentName" :placeholder="'文件名称'" class="filter-item search-item" />

      <el-button class="filter-item" type="primary" plain @click="search">
        查询
      </el-button>
      <el-button class="filter-item" type="warning" plain @click="reset">
        重置
      </el-button>
      <el-upload
        class="upload"
        :action="uploadAction"
        :headers="headers"
        list-type="picture"
        :show-file-list="false"
        :before-upload="beforeUpload"
        :on-success="uploadSuccess"
        :on-error="uploadError"
        :on-progress="uploadProgress"
      >
        <el-button class="filter-item" icon="el-icon-upload2" type="success" plain>
          {{ '上传' }}
        </el-button>
      </el-upload>
    </div>
    <el-table
      ref="table"
      :key="tableKey"
      v-loading="loading"
      :data="list"
      border
      fit
      style="width: 100%;"
    >
      <el-table-column :label="'文件标识'" prop="id" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'文件名称'" prop="documentName" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.documentName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'归属业务标识'" prop="busiDocappId" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.busiDocappId }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'归属业务'" prop="busiDocappType" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.busiDocappType }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'文件类型'" prop="documentAttach" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.documentAttach }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'存放地址'" prop="urlAddress" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.urlAddress }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'创建时间'" prop="createDate" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createDate }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'操作'" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button icon="el-icon-download" type="primary" size="mini" bound @click="download(scope.row.id,scope.row.documentName)">下载</el-button>
          <el-button icon="el-icon-delete" type="danger" size="mini" bound @click="deleteById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="pagination.num" :limit.sync="pagination.size" @pagination="fetch" />
  </div>
</template>
<script>
import Pagination from '@/components/Pagination';
import { getToken } from '@/utils/auth';
import { getFileType } from '@/utils';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

export default {
  name: 'ExImport',
  components: { Pagination },
  data() {
    return {
      uploadAction: `${process.env.VUE_APP_BASE_API}system/document/upload`,
      tableKey: 0,
      loading: false,
      list: null,
      total: 0,
      queryParams: {},
      pagination: {
        size: 10,
        num: 1
      },
      fileList: [],
      headers: {
        Authorization: `bearer ${getToken()}`
      },
      data: [],
      error: [],
      time: '0 ms'
    };
  },
  mounted() {
    this.fetch();
  },
  methods: {
    search() {
      this.fetch({
        ...this.queryParams
      });
    },
    reset() {
      this.params = {};
      this.queryParams = {};
      this.$refs.table.clearSort();
      this.$refs.table.clearFilter();
      this.fetch();
    },
    beforeUpload(file) {},
    uploadError() {
      this.$message({
        message: this.$t('tips.uploadFailed'),
        type: 'error'
      });
      NProgress.done();
    },
    uploadSuccess(response) {
      this.data = response.resultBody;
      NProgress.done();
      this.fetch();
      this.$message({
        message: '上传成功',
        type: 'success'
      });
    },
    uploadProgress() {
      NProgress.start();
    },
    fetch(params = {}) {
      this.loading = true;
      params.pageSize = this.pagination.size;
      params.pageNum = this.pagination.num;
      this.$post('system/document/getDocumentList', { ...params }).then(r => {
        const data = r.data.resultBody;
        this.total = data.total;
        this.list = data.records;
        this.loading = false;
      });
    },
    download(docId, name) {
      this.$download('system/document/downloadDoc', {
        docId: docId
      }, `${name}`)
    },
    deleteById(docId) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$get(`system/document/deleteDoc?docId=${docId}`).then(r => {
            if (r.data.code == '000000') {
              this.$message({
                message: '删除成功',
                type: 'success'
              });
              this.fetch();
            }
            this.loading = false;
          });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.upload {
  display: inline-block;
}
</style>
